package com.hspedu.newData.dmsxl.单调栈;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-23 
 * @Version: 1.0
 */

public class LC42接雨水 {

    // 左右数组
    public int trap(int[] height) {
        int len = height.length;
        int[] left = new int[len];
        int[] right = new int[len];

        left[0] = height[0];
        right[len-1] = height[len - 1];
        for (int i = 1; i < len; i++) {
            left[i] = Math.max(left[i-1],height[i]);
        }
        for (int i = len - 2; i >= 0; i--) {
            right[i] = Math.max(right[i+1],height[i]);
        }

        int sum = 0;
        for (int i = 0; i < len; i++) {
            sum += Math.min(left[i],right[i]) - height[i];
        }
        return sum;
    }

    // todo 单调栈

    @Test
    public void test() {
        // 测试代码
        int trap = trap(new int[]{4,2,0,3,2,5});
    }
}
